/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servelt;

import DatabaseAccessing.AddUserException;
import DatabaseAccessing.Customer;
import DatabaseAccessing.UserModelController;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author MaramZahran
 */
public class Signup extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    private UserModelController userModel;
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Signup</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Signup at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException ,ServletException
    {

        userModel=new UserModelController();
        RequestDispatcher rd =null;
        Customer customer=(Customer)request.getSession().getAttribute("signupBean"); //beans that contains users data
        request.getSession().removeAttribute("signupBean");
          //PrintWriter out=response.getWriter();
        // if save complete redirect to signupComplete  page
        try
        {
        userModel.addNewConstumer(customer);
        //out.println(customer.getCustomerEmail());
        //out.println(customer.getCredit());
        //out.println(customer.getCustomerName());
        rd=request.getRequestDispatcher("SignupComplete.jsp");
        //(request, response);
        request.getSession().setAttribute("msgfromserver","Registration Complete");

         request.getSession().removeAttribute("ErrorMsg");
         request.getSession().removeAttribute("email");
         request.getSession().removeAttribute("name");
         request.getSession().removeAttribute("phone");
         request.getSession().removeAttribute("credit");




        response.sendRedirect("SignupComplete.jsp");
        // rd.forward(request, response);
        //response.sendRedirect("SignupComplete.jsp");

        }

        // if errror occurs as user found or any data exception catch by
        // user exception and redirect to signup page with apporiate message
        catch(AddUserException ex)
        {
         rd = request.getRequestDispatcher("Signup.jsp");
         request.getSession().setAttribute("ErrorMsg", ex.exceptionNumber());
         request.getSession().setAttribute("email",customer.getCustomerEmail());
         request.getSession().setAttribute("name", customer.getCustomerName());
         request.getSession().setAttribute("phone", customer.getPhoneNumber());
         request.getSession().setAttribute("credit", customer.getCredit());

         response.sendRedirect("Signup.jsp");
        // rd.forward(request,response);
        }
    }
    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }

}
